package com.molichuxing.baselibrary.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.molichuxing.baselibrary.bean.PaySNInfo;
import com.molichuxing.baselibrary.net.OkHttpManager;

import java.util.Locale;

/**
 * 手机设备信息获取的工具类
 */
public class DeviceUtiles {
    private static String s;

    public static PaySNInfo.DataBean getPosSN_db(Context mContext) {
        try {
            String jsStr = SharePreUtil.GetShareString(mContext,SharePreUtil.DEVICE_PAY);
            if(TextUtils.isEmpty(jsStr)){
                LogUtils.i("dove","支付参数信息为null");
                return null;
            }
            PaySNInfo posSN = OkHttpManager.getInstance().getmGson().fromJson(jsStr,PaySNInfo.class);
            PaySNInfo.DataBean posSN_db = posSN.getData();
            return posSN_db;
        }catch (Exception e){
            LogUtils.i("dove","支付参数信息解析异常");
            e.printStackTrace();
            return null;
        }
    }

    public static void setPosSN_db(Context mContext, String payJson) {
        SharePreUtil.SetShareString(mContext,SharePreUtil.DEVICE_PAY,payJson);
    }

    /**
     * 获取设备的imei号
     *
     * @param context context
     * @return imei
     */
    public static String getIMEI(Context context) {

        TelephonyManager mTelephonyMgr
                = (TelephonyManager) context.getSystemService(
                Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return "";
        }
        String imei = mTelephonyMgr.getDeviceId();
        if (TextUtils.isEmpty(imei) || imei.equals("000000000000000")) {
            imei = "0";
        }
        return imei;
    }


    /**
     * 获取sdk版本
     */
    public static int getAndroidOSVersion() {

        int osVersion;
        try {
            osVersion = Integer.valueOf(android.os.Build.VERSION.SDK);
        } catch (NumberFormatException e) {
            osVersion = 0;
        }

        return osVersion;
    }


    /**
     * 获取手机的具体信息
     * @return
     */
    public static String getHandSetInfo() {

        String handSetInfo = "手机型号:" + android.os.Build.MODEL +
                ",SDK版本:" + android.os.Build.VERSION.SDK +
                ",系统版本:" + android.os.Build.VERSION.RELEASE;
        return handSetInfo;
    }


    /**获取手机的详细版本号
     *
     * @return
     */
    public static String getdeviceVersion() {
        String handSetInfo =  android.os.Build.VERSION.RELEASE;
        return handSetInfo;
    }


    /**
     * 获取imei加密后的数据
     *
     * @param context context
     * @return imei
     */
    public static String getMd5Imei(Context context) {
        String imei = getIMEI(context);
        String lowerCase = imei.toLowerCase(Locale.getDefault());
        try {
            s = Md5Utils.md5Encode(lowerCase);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

    /**
     * 获取渠道名
     * @param ctx 此处习惯性的设置为activity，实际上context就可以
     * @return 如果没有获取成功，那么返回值为空
     */
    public static String getChannelName(Activity ctx) {
        if (ctx == null) {
            return "";
        }
        String channelName = "";
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                //注意此处为ApplicationInfo 而不是 ActivityInfo,因为友盟设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = applicationInfo.metaData.getString("");
                    }
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return channelName;
    }

    /**
     * 获取application中指定的meta-data
     * @return 如果没有获取成功(没有对应值，或者异常)，则返回值为空
     */
    public static String getAppMetaData(Context ctx, String key) {
        if (ctx == null || TextUtils.isEmpty(key)) {
            return "";
        }
        String resultData = "";
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        resultData = applicationInfo.metaData.getString(key);
                    }
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return resultData;
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

}
